library(plotly)
library(ggplot2)
library(grid)
#install.packages("gridExtra")
library(gridExtra)
#Age
ggplot(graphs) + geom_bar(aes(x = graphs$AGE_CAT), color = "navy", fill = "navy")+
xlab("Age") + ylab("Number of Prisoners") + ggtitle("Age Distribution")
#Race
graphs$RACE <- full.numeric$RACE
ggplot(graphs) + geom_bar(aes(x = graphs$RACE), color = "navy", fill = "navy")+
xlab("Race") + ylab("Number of Prisoners") + ggtitle("Distribution of Race") +
scale_x_discrete(labels=c("White", "Black", "Hispanic", "Indian", "Asian", "Multiple", "Missing"))
#Gender
ggplot(graphs) + geom_bar(aes(x = graphs$Gender), color = "navy", fill = "navy")+
xlab("Gender") + ylab("Number of Prisoners") + ggtitle("Gender")
#Education
graphs$Education <- as.integer(graphs$Education)
ggplot(graphs) + geom_bar(aes(x = graphs$Education), color = "navy", fill = "navy")+
xlab("Education") + ylab("Number of Prisoners") + ggtitle("Education")
NA
#Recidivism
ggplot(graphs) + geom_bar(aes(x = graphs$CH_CRIMHIST_COLLAPSED), color = "navy", fill = "navy")+
xlab("Recidivism") + ylab("Number of Prisoners") + ggtitle("Recidivism")
#Prisoners with Incarcerated Parents
ggplot(graphs) + geom_bar(aes(x = graphs$SES_PARENTS_INCARCERATED), color = "navy", fill = "navy")+
xlab("Parents Incarcerated") + ylab("Number of Prisoners") + ggtitle("Prisoners with Incarcerated Parents")
#Prisoners with Incarcerated Family Members
ggplot(graphs) + geom_bar(aes(x = graphs$SES_FAMILY_INCARCERATED), color = "navy", fill = "navy")+
xlab("Family Incarcerated") + ylab("Number of Prisoners") + ggtitle("Prisoners with Incarcerated Family Members")
#Length of Sentence
ggplot(graphs) + geom_density(aes(x = graphs$CS_SENTENCEMTH), color = "navy", fill = "navy")+
xlab("Sentence in Number of Months") + ylab("Number of Prisoners") + ggtitle("Length of Sentence") +
coord_cartesian(xlim=c(0, 2000))
#Violent Offenses
ggplot(graphs) + geom_bar(aes(x = graphs$OFFENSE_VIOLENT), color = "navy", fill = "navy")+
xlab("Violent Offenses") + ylab("Number of Prisoners") + ggtitle("Violent Offenses")
#Drug Offenses
ggplot(graphs) + geom_bar(aes(x = graphs$OFFENSE_DRUG), color = "navy", fill = "navy")+
xlab("Drug Offenses") + ylab("Number of Prisoners") + ggtitle("Drug Offenses")
#Use Of Any Illegal Drug Regularly
ggplot(graphs) + geom_bar(aes(x = graphs$DRUG_ANYREG), color = "navy", fill = "navy")+
xlab("Illegal Drug Use") + ylab("Number of Prisoners") + ggtitle("Use Of Any Illegal Drug Regularly")
#Sexual Abuse
ggplot(graphs) + geom_bar(aes(x = graphs$SES_SEXABUSED_EVER), color = "navy", fill = "navy")+
xlab("Whether Sexually Abused") + ylab("Number of Prisoners") + ggtitle("Sexual Abuse")
#Physically Abused
ggplot(graphs) + geom_bar(aes(x = graphs$SES_PHYSABUSED_EVER), color = "navy", fill = "navy")+
xlab("Physically Abused") + ylab("Number of Prisoners") + ggtitle("Physical Abuse")
#plotly
p <- plot_ly(x = graphs$SES_SEXABUSED_EVER, type = "histogram", name = 'Sexual Abuse') %>%
add_histogram(x = graphs$SES_PHYSABUSED_EVER, name = 'Physical Abuse')%>%
layout(title = "Sexual and Physical Abuse",
xaxis = list(title = "Type of Abuse"),
yaxis = list(title = "Number of Prisoners"))
p
Ignoring 290 observationsIgnoring 250 observationsIgnoring 290 observationsIgnoring 250 observations
#Prisoners with Children
ggplot(graphs) + geom_bar(aes(x = graphs$SES_HASCHILDREN), color = "navy", fill = "navy")+
xlab("Prisoners with Children") + ylab("Number of Prisoners") + ggtitle("Prisoners with Children")
#Prisoners with Minor Children
graphs$SES_NUMOFMINOR_CHILDREN <- full.numeric$SES_NUMOFMINOR_CHILDREN
graphs$SES_NUMOFMINOR_CHILDREN <- as.factor(graphs$SES_NUMOFMINOR_CHILDREN)
graphs$SES_NUMOFMINOR_CHILDREN <- as.integer(graphs$SES_NUMOFMINOR_CHILDREN)
graphs$SES_NUMOFMINOR_CHILDREN <- as.character(graphs$SES_NUMOFMINOR_CHILDREN)
ggplot(graphs) + geom_bar(aes(x = graphs$SES_NUMOFMINOR_CHILDREN), color = "navy", fill = "navy")+
xlab("Number of Minor Children") + ylab("Number of Prisoners") + ggtitle("Prisoners with Minor Children")+
scale_x_discrete(labels=c("0", "1", "2", "3", "4", "5", "6"))
#Length of Sentence and Recidivism
ggplot(graphs,aes(x=CS_SENTENCEMTH,y=CH_CRIMHIST_COLLAPSED)) +
xlab("Length of Sentence") +
ylab("Recidivism") +
geom_point(alpha = 0.1, colour="navy",size=1) +
geom_smooth() + xlab("Length of Sentence") +
ylab("Recidivism") +
ggtitle("Length of Sentence and Recidivism")
#Length of Sentence and Violent Offences
ggplot(graphs,aes(x=CS_SENTENCEMTH,y=OFFENSE_VIOLENT)) +
xlab("Length of Sentence") +
ylab("Violent Crimes") +
geom_point(alpha = 0.1, colour="navy",size=1) +
geom_smooth() + xlab("Length of Sentence") +
ylab("Violent Crimes") +
ggtitle("Length of Sentence and Violent Offences")
#Length of Sentence and Drug Offences
ggplot(graphs,aes(x=CS_SENTENCEMTH,y=OFFENSE_DRUG)) +
xlab("Length of Sentence") +
ylab("Drug Crimes") +
geom_point(alpha = 0.1, colour="navy",size=1) +
geom_smooth() + xlab("Length of Sentence") +
ylab("Drug Crimes") +
ggtitle("Length of Sentence and Drug Offences")
#Recidivism and Race
graphs$recidivism <- as.character(graphs$CH_CRIMHIST_COLLAPSED)
graphs$recidivism[graphs$recidivism == 0] <- NA
graphs$recidivism <- as.numeric(graphs$recidivism)
graphs$RACE <- as.numeric(graphs$RACE)
graphs$rac.rec <- graphs$RACE*graphs$recidivism
graphs$rac.rec <- as.factor(graphs$rac.rec)
rac.rec1 <- as.data.frame(na.omit(graphs$rac.rec))
ggplot(rac.rec1) + geom_bar(aes(x = rac.rec1$`na.omit(graphs$rac.rec)`), color = "navy", fill = "navy")+ xlab("Race") + ylab("Number of Recidivist Prisoners") + ggtitle("Race and Recidivism") +
scale_x_discrete(labels=c("White", "Black", "Hispanic", "Indian", "Asian", "Multiple", "Missing"))
#Education and Recidivism
graphs$Education <- as.numeric(graphs$Education)
graphs$Ed.rec <- graphs$Education*graphs$recidivism
graphs$Ed.rec <- as.factor(graphs$Ed.rec)
Ed.rec1 <- as.data.frame(na.omit(graphs$Ed.rec))
#graph1
ggplot(Ed.rec1) + geom_bar(aes(x = Ed.rec1$`na.omit(graphs$Ed.rec)`), color = "navy", fill = "navy")+ xlab("Education") + ylab("Number of Recidivist Prisoners") + ggtitle("Education and Recidivism")
#graph2
ggplot(Ed.rec1) + geom_density(aes(x = Ed.rec1$`na.omit(graphs$Ed.rec)`), color = "navy", fill = "navy")+ xlab("Education") + ylab("Number of Recidivist Prisoners") + ggtitle("Education and Recidivism")
#Different graphs?
#Recidivism and Drug Crimes
graphs$OFFENSE_DRUG <- as.numeric(graphs$OFFENSE_DRUG)
graphs$Drug.rec <- graphs$OFFENSE_DRUG*graphs$recidivism
graphs$Drug.rec <- as.factor(graphs$Drug.rec)
Drug.rec1 <- as.data.frame(na.omit(graphs$Drug.rec))
ggplot(Drug.rec1) + geom_bar(aes(x = Drug.rec1$`na.omit(graphs$Drug.rec)`), color = "navy", fill = "navy")+ xlab("Drug Crimes") + ylab("Number of Recidivist Prisoners") + ggtitle("Drug Crimes and Recidivism")
#Recidivism and Violent Crimes
graphs$OFFENSE_VIOLENT <- as.numeric(graphs$OFFENSE_VIOLENT)
graphs$Violent.rec <- graphs$OFFENSE_VIOLENT*graphs$recidivism
graphs$Violent.rec <- as.factor(graphs$Violent.rec)
Violent.rec1 <- as.data.frame(na.omit(graphs$Violent.rec))
ggplot(Violent.rec1) + geom_bar(aes(x = Violent.rec1$`na.omit(graphs$Violent.rec)`), color = "navy", fill = "navy")+ xlab("Violent Crimes") + ylab("Number of Recidivist Prisoners") + ggtitle("Violent Crimes and Recidivism")
#Plotly
p <- plot_ly(x = graphs$Drug.rec, type = "histogram", name = 'Drug Crimes') %>%
add_histogram(x = graphs$Violent.rec, name = 'ViolentCrimes')%>%
layout(title = "Recidivism and Type of Crimes",
xaxis = list(title = "Recidivism"),
yaxis = list(title = "Number of Prisoners"))
p
Ignoring 6042 observationsIgnoring 6042 observationsIgnoring 6042 observationsIgnoring 6042 observations
load("~/Desktop/model-errors.rdata")
p <- plot_ly(model.errors, y = ~error.full, type = 'scatter', mode = 'scatter', name = 'Full',
line = list(color = 'rgb(205, 12, 24)', width = 4)) %>%
add_trace(y = ~error.train, name = 'Train', line = list(color = 'rgb(22, 96, 167)', width = 4)) %>%
add_trace(y = ~error.test, name = 'Test', line = list(color = 'rgb(30,144,70)', width = 4)) %>%
add_trace(y = ~error.val, name = 'Validation', line = list(color = 'rgb(175, 90, 166)', width = 4)) %>%
layout(title = "Error Lines",
xaxis = list(title = "Models"),
yaxis = list (title = "Mean Error Score"))
replacing previous import by ‘shiny::includeHTML’ when loading ‘crosstalk’replacing previous import by ‘shiny::knit_print.shiny.tag’ when loading ‘crosstalk’replacing previous import by ‘shiny::code’ when loading ‘crosstalk’replacing previous import by ‘shiny::includeScript’ when loading ‘crosstalk’replacing previous import by ‘shiny::includeMarkdown’ when loading ‘crosstalk’replacing previous import by ‘shiny::tags’ when loading ‘crosstalk’replacing previous import by ‘shiny::is.singleton’ when loading ‘crosstalk’replacing previous import by ‘shiny::withTags’ when loading ‘crosstalk’replacing previous import by ‘shiny::img’ when loading ‘crosstalk’replacing previous import by ‘shiny::tagAppendAttributes’ when loading ‘crosstalk’replacing previous import by ‘shiny::knit_print.shiny.tag.list’ when loading ‘crosstalk’replacing previous import by ‘shiny::knit_print.html’ when loading ‘crosstalk’replacing previous import by ‘shiny::tagAppendChild’ when loading ‘crosstalk’replacing previous import by ‘shiny::includeCSS’ when loading ‘crosstalk’replacing previous import by ‘shiny::br’ when loading ‘crosstalk’replacing previous import by ‘shiny::singleton’ when loading ‘crosstalk’replacing previous import by ‘shiny::span’ when loading ‘crosstalk’replacing previous import by ‘shiny::a’ when loading ‘crosstalk’replacing previous import by ‘shiny::tagList’ when loading ‘crosstalk’replacing previous import by ‘shiny::strong’ when loading ‘crosstalk’replacing previous import by ‘shiny::tag’ when loading ‘crosstalk’replacing previous import by ‘shiny::p’ when loading ‘crosstalk’replacing previous import by ‘shiny::validateCssUnit’ when loading ‘crosstalk’replacing previous import by ‘shiny::HTML’ when loading ‘crosstalk’replacing previous import by ‘shiny::h1’ when loading ‘crosstalk’replacing previous import by ‘shiny::h2’ when loading ‘crosstalk’replacing previous import by ‘shiny::h3’ when loading ‘crosstalk’replacing previous import by ‘shiny::h4’ when loading ‘crosstalk’replacing previous import by ‘shiny::h5’ when loading ‘crosstalk’replacing previous import by ‘shiny::h6’ when loading ‘crosstalk’replacing previous import by ‘shiny::tagAppendChildren’ when loading ‘crosstalk’replacing previous import by ‘shiny::em’ when loading ‘crosstalk’replacing previous import by ‘shiny::div’ when loading ‘crosstalk’replacing previous import by ‘shiny::pre’ when loading ‘crosstalk’replacing previous import by ‘shiny::htmlTemplate’ when loading ‘crosstalk’replacing previous import by ‘shiny::suppressDependencies’ when loading ‘crosstalk’replacing previous import by ‘shiny::tagSetChildren’ when loading ‘crosstalk’replacing previous import by ‘shiny::includeText’ when loading ‘crosstalk’replacing previous import by ‘shiny::hr’ when loading ‘crosstalk’
p
A line object has been specified, but lines is not in the mode
Adding lines to the mode...
A line object has been specified, but lines is not in the mode
Adding lines to the mode...
A line object has been specified, but lines is not in the mode
Adding lines to the mode...
A line object has been specified, but lines is not in the mode
Adding lines to the mode...
A line object has been specified, but lines is not in the mode
Adding lines to the mode...
A line object has been specified, but lines is not in the mode
Adding lines to the mode...
A line object has been specified, but lines is not in the mode
Adding lines to the mode...
A line object has been specified, but lines is not in the mode
Adding lines to the mode...
load("~/Desktop/model-errors.rdata")
p <- plot_ly(model.errors, y = ~error.full, type = 'scatter', mode = 'scatter', name = 'Full',
line = list(color = 'rgb(205, 12, 24)', width = 4)) %>%
add_trace(y = ~error.train, name = 'Train', line = list(color = 'rgb(22, 96, 167)', width = 4)) %>%
add_trace(y = ~error.test, name = 'Test', line = list(color = 'rgb(30,144,70)', width = 4)) %>%
add_trace(y = ~error.val, name = 'Validation', line = list(color = 'rgb(175, 90, 166)', width = 4)) %>%
layout(title = "Error Lines",
xaxis = list(title = "Models"),
yaxis = list (title = "Mean Error Score"))
p
boxplot(CH_CRIMHIST_COLLAPSED~RACE, data=graphs, notch=TRUE, col=(c(“gold”,“darkgreen”)), main=“Recidivism and Race”, xlab=“”) ggplot(graphs,aes(x=RACE,y=CH_CRIMHIST_COLLAPSED)) + xlab(“Race”) + ylab(“Recidivism”) + geom_bar(alpha = 0.1, colour=“navy”,size=1) + geom_smooth() + xlab(“Race”) + ylab(“Recidivism”) + ggtitle(“Recidivism and Race”)
‘scatter’, ‘box’, ‘bar’, ‘heatmap’, ‘histogram’, ‘histogram2d’, ‘histogram2dcontour’, ‘pie’, ‘contour’, ‘scatterternary’, ‘scatter3d’, ‘surface’, ‘mesh3d’, ‘scattergeo’, ‘choropleth’, ‘scattergl’, ‘pointcloud’, ‘heatmapgl’, ‘parcoords’, ‘scattermapbox’, ‘carpet’, ‘scattercarpet’, ‘contourcarpet’, ‘ohlc’, ‘candlestick’, ‘area’ ```
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).